package rpc.client.socket;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.CloseFuture;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import rpc.CommonStaticSource;

public class MinaClientSocket {

    private static SocketConnector connector;
    private static ConnectFuture future;
    private static IoSession session;
    
    static {
        /*
         * 1.创建一个socket连接,连接到服务器
         */
        connector = new NioSocketConnector();

        /*
         * 获取过滤器链,用于添加过滤器
         */
        DefaultIoFilterChainBuilder chain = connector.getFilterChain();

        // b.添加日志过滤器
        chain.addLast("logger", new LoggingFilter());

        // c.添加字符的编码过滤器
        chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

        /*
         * 3.设置消息处理器，用于处理接收到的消息
         */
        connector.setHandler(new SendHandler());

        /*
         * 4.根据IP和端口号连接到服务器
         */
        future = connector.connect(new InetSocketAddress(ClientSocket.IP_ADD, CommonStaticSource.SOCKET_PORT));
        // 等待连接创建完成
        future.awaitUninterruptibly();

        /*
         * 5.获取session对象,通过session可以向服务器发送消息；
         */
        session = future.getSession();
        session.getConfig().setUseReadOperation(true);
    }
    
    /**
     * 往服务器发送消息
     * 
     * @param message
     */
    public static void sendMsg2Server(Object message) {
        session.write(message);
    }
    
    /**
     * 关闭与服务器的连接
     * 
     * @return
     */
    public boolean close() {
        CloseFuture future = session.getCloseFuture();
        future.awaitUninterruptibly(1000);
        connector.dispose();
        return true;
    }
}
